import renderMarkdown from "./render/markdown.js";
import renderVersion from "./render/version.js";
import renderToc from "./render/toc.js";
import eventInit from "./event/jsEvent.js";
import { Version } from "./all.js";
import { getContent } from "./communicate/AjaxPart.js";
window.Setting = {};

window.onload = async function () {
    // 网页初步加载
    eventInit();
    hljs.initHighlightingOnLoad();

    // 载入 document.json
    let npm = await fetch("./document.json").then((res) => res.json());

    let { doc, path, version } = Object.fromEntries(new URL(location.href).searchParams.entries());
    console.log({ doc, path, version });
    let docName = doc || Object.keys(npm)[0];

    var { defaultFile, root, packageName, title } = npm[docName];
    document.head.querySelector("title").innerHTML = title;
    let text = await getContent(packageName, version, root + (path || defaultFile));
    renderMarkdown(text);

    let version1 = await renderVersion(packageName);

    let selectVersion = version || version1;
    Version.selectedIndex = [...Version.children].reduce((a, b, index) => (b.innerText == selectVersion ? index : a), -1);

    renderToc(packageName, version || version1, root);

    //重要数据载入window
    Object.assign(window.Setting, { packageName, version: version || version1, path: path || defaultFile, doc: docName, root });
};
